﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Windows.Forms;
using DatabaseCore;
using DatabaseCore.Dto;
using Eaktest;
using SystemCore;

namespace EakTest
{
    public partial class QueueList : BaseForm
    {
        private IProdQueueDao prodQueueDao = new ProdQueueDao();
        private IList<ProdQueue> ProdQueueSource { get; set; }
        private BindingSource bs { get; set; }

        public QueueList()
        {
            InitializeComponent();

            bs = new BindingSource();

            GetAllProductSummary();
            BindData();
        }

        #region Private method

        private void GetAllProductSummary()
        {
            ProdQueueSource = prodQueueDao.GetAllQueues();

            bs.Clear();
            foreach (var item in ProdQueueSource)
            {
                bs.Add(item);
            }
        }

        private void ClearGridView()
        {
            gvQueueList.DataSource = null;
        }

        private void BindData()
        {
            ClearGridView();

            gvQueueList.DataSource = bs;
        }

        private void UpdatePause(bool isPause, string username)
        {
            if (gvQueueList.CurrentRow.Index == -1)
                return;

            int id = Convert.ToInt32(gvQueueList["id", gvQueueList.CurrentRow.Index].Value);

            bool isComplete = prodQueueDao.UpdateQueuePause(id, username, isPause);
            if (!isComplete)
            {
                MessageDialog.Error("Something wrong!!");
                return;
            }

            GetAllProductSummary();
            BindData();
        }

        private void UpdateQueuePriority()
        {
            ProdQueueSource= (IList<ProdQueue>)bs.List;
            bool isComplete = prodQueueDao.UpdateQueuePriority(ProdQueueSource);
            if (!isComplete)
            {
                MessageDialog.Error("Something wrong!!");
                return;
            }
        }

        #endregion

        #region Event

        private void btnUp_Click(object sender, EventArgs e)
        {
            int position = bs.Position;
            if (position == 0)
            {
                MessageDialog.Information("Already Top Item!");
                return;
            }

            bs.RaiseListChangedEvents = false;

            ProdQueue previous = (ProdQueue)bs[position - 1];

            ProdQueue current = (ProdQueue)bs.Current;
            bs.Remove(current);

            // สลับ priority กับคิวก่อนหน้า
            previous.Priority += 1;
            current.Priority -= 1;

            position--;

            bs.Insert(position, current);
            bs.Position = position;

            bs.RaiseListChangedEvents = true;
            bs.ResetBindings(false);

            UpdateQueuePriority();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            int position = bs.Position;
            if (position == bs.Count - 1)
            {
                MessageDialog.Information("Already Bottom Item!");
                return; 
            }

            bs.RaiseListChangedEvents = false;

            ProdQueue next = (ProdQueue)bs[position + 1];
            
            ProdQueue current = (ProdQueue)bs.Current;
            bs.Remove(current);

            // สลับ priority กับคิวหลัง
            next.Priority -= 1;
            current.Priority += 1;

            position++;

            bs.Insert(position, current);
            bs.Position = position;

            bs.RaiseListChangedEvents = true;
            bs.ResetBindings(false);

            UpdateQueuePriority();
        }

        private void btnActive_Click(object sender, EventArgs e)
        {
            UpdatePause(false, "test");
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            UpdatePause(true, "test");
        }

        #endregion

    }
}